const router_koa = require('koa-router')
const router=new router_koa()
const path=require('path')
const fs=require('fs')
const app=require('./app')

const { createBundleRenderer } = require('vue-server-renderer')
const template=fs.readFileSync(path.resolve(__dirname,'../src/index.html'),'utf-8')
const clientManifest = require('../dist/vue-ssr-client-manifest.json')
const renderer = createBundleRenderer(path.resolve(__dirname,'../dist/vue-ssr-server-bundle.json'), {
    runInNewContext: false,
    template,
    clientManifest
})

router.get('/api/1',async (ctx)=>{
    ctx.body={
        id:588
    }
})

router.get('/(.*)',async (ctx)=>{
        const context = { url: ctx.url }
        await renderer.renderToString(context).then(html=>{
           ctx.body=html
        })   
})

module.exports = [
    router.routes(),
    router.allowedMethods()
]
